<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="### android中使用SQLiteOpenHelper这个辅助类时，可以生成一个数据库，并可以对数据库进行管理的方法可以是?  A. getWriteableDatabase()B. getReadableDatabase()C. getDatabase()D. getAbleDatabase()  答案：AB这道题的其实不算难，纯猜也可以猜出来是AB。但是我以前一直以为getReadab">
<meta property="og:type" content="article">
<meta property="og:title" content="从源码来看getReadableDatabase()的误区">
<meta property="og:url" content="http://vchao.top/2017/05/12/%E4%BB%8E%E6%BA%90%E7%A0%81%E6%9D%A5%E7%9C%8BgetReadableDatabase()%E7%9A%84%E8%AF%AF%E5%8C%BA/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="### android中使用SQLiteOpenHelper这个辅助类时，可以生成一个数据库，并可以对数据库进行管理的方法可以是?  A. getWriteableDatabase()B. getReadableDatabase()C. getDatabase()D. getAbleDatabase()  答案：AB这道题的其实不算难，纯猜也可以猜出来是AB。但是我以前一直以为getReadab">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2017-05-12T12:03:39.000Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.798Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://vchao.top/2017/05/12/%E4%BB%8E%E6%BA%90%E7%A0%81%E6%9D%A5%E7%9C%8BgetReadableDatabase()%E7%9A%84%E8%AF%AF%E5%8C%BA/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>从源码来看getReadableDatabase()的误区 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2017/05/12/%E4%BB%8E%E6%BA%90%E7%A0%81%E6%9D%A5%E7%9C%8BgetReadableDatabase()%E7%9A%84%E8%AF%AF%E5%8C%BA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          从源码来看getReadableDatabase()的误区
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2017-05-12 20:03:39" itemprop="dateCreated datePublished" datetime="2017-05-12T20:03:39+08:00">2017-05-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-08-25 22:37:20" itemprop="dateModified" datetime="2021-08-25T22:37:20+08:00">2021-08-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>﻿### android中使用SQLiteOpenHelper这个辅助类时，可以生成一个数据库，并可以对数据库进行管理的方法可以是?</p>
<blockquote>
<p>A. getWriteableDatabase()<br>B. getReadableDatabase()<br>C. getDatabase()<br>D. getAbleDatabase()</p>
</blockquote>
<p>答案：AB<br>这道题的其实不算难，纯猜也可以猜出来是AB。但是我以前一直以为getReadableDatabase()是以只读方式打开数据库。这就尴尬了。</p>
<h4 id="解析"><a href="#解析" class="headerlink" title="解析"></a>解析</h4><p>Android使用 getWritableDatabase() 和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。(getReadableDatabase()方法中会调用getWritableDatabase()方法)<br><strong>getReadableDatabase()并不是以只读方式打开数据库，而是先执行getWritableDatabase()，失败的情况下才以只读方式打开数据库.。</strong><br>但getWritableDatabase()方法以读写方式打开数据库，一旦数据库的磁盘空间满了，数据库就只能读而不能写，<br>getWritableDatabase()打开数据库就会出错。getReadableDatabase()方法先以读写方式打开数据库，<br>倘若使用如果数据库的磁盘空间满了，就会打开失败，当打开失败后会继续尝试以只读方式打开数据库。</p>
<h4 id="这是我翻译之后的"><a href="#这是我翻译之后的" class="headerlink" title="这是我翻译之后的~~"></a>这是我翻译之后的~~</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">	* 创建或者打开一个用于读写的数据库。一旦成功打开，数据库就会被缓存。所以每次你想写入数据库时，都需要调用此方法。当你不再需要时，请确保调用&#123;@link #close&#125;关闭这个数据库。如果权限不足或数据库的磁盘空间满了，可能导致此操作失败，但如果问题是固定的，未来的尝试可能会成功。</span><br><span class="line">	* @throws 如果数据库不能用于写入，则抛出SQLiteException。</span><br><span class="line">	* @return 返回一个读/写数据库对象，直到调用&#123;@link #close&#125;关闭数据库。</span><br><span class="line">    */</span><br><span class="line">    public synchronized SQLiteDatabase getWritableDatabase() &#123;</span><br><span class="line">        if (mDatabase != null &amp;&amp; mDatabase.isOpen() &amp;&amp; !mDatabase.isReadOnly()) &#123;</span><br><span class="line">            return mDatabase;  // 数据库已经被打开了。</span><br><span class="line">        &#125;</span><br><span class="line">        if (mIsInitializing) &#123;</span><br><span class="line">            throw new IllegalStateException(&quot;getWritableDatabase called recursively&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        // 如果我们打开了一个只读数据库，别人就可以使用它(即使他们不应该)，这会导致文件上被锁定，我们打开数据库读写的尝试将会失败，等待文件锁定。为了防止这种情况发生，我们获取了只读数据库上的锁，让它关闭其他用户。</span><br><span class="line">		</span><br><span class="line">        boolean success = false;</span><br><span class="line">        SQLiteDatabase db = null;</span><br><span class="line">        if (mDatabase != null) mDatabase.lock();</span><br><span class="line">        try &#123;</span><br><span class="line">            mIsInitializing = true;</span><br><span class="line">            if (mName == null) &#123;</span><br><span class="line">                db = SQLiteDatabase.create(null);</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                db = mContext.openOrCreateDatabase(mName, 0, mFactory);</span><br><span class="line">            &#125;</span><br><span class="line">            int version = db.getVersion();</span><br><span class="line">            if (version != mNewVersion) &#123;</span><br><span class="line">                db.beginTransaction();</span><br><span class="line">                try &#123;</span><br><span class="line">                    if (version == 0) &#123;</span><br><span class="line">                        onCreate(db);</span><br><span class="line">                    &#125; else &#123;</span><br><span class="line">                        onUpgrade(db, version, mNewVersion);</span><br><span class="line">                    &#125;</span><br><span class="line">                    db.setVersion(mNewVersion);</span><br><span class="line">                    db.setTransactionSuccessful();</span><br><span class="line">                &#125; finally &#123;</span><br><span class="line">                    db.endTransaction();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            onOpen(db);</span><br><span class="line">            success = true;</span><br><span class="line">            return db;</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            mIsInitializing = false;</span><br><span class="line">            if (success) &#123;</span><br><span class="line">                if (mDatabase != null) &#123;</span><br><span class="line">                    try &#123; mDatabase.close(); &#125; catch (Exception e) &#123; &#125;</span><br><span class="line">                    mDatabase.unlock();</span><br><span class="line">                &#125;</span><br><span class="line">                mDatabase = db;</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                if (mDatabase != null) mDatabase.unlock();</span><br><span class="line">                if (db != null) db.close();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">	 * 创建或打开一个数据库。这将和&#123;@link #getWritableDatabase&#125;返回相同的对象，除非出现某个问题(如磁盘空间已满)数据库只能打开为只读。在这种情况下，将返回一个只读数据库对象。如果问题是固定的，将来调用&#123;@link #getWritableDatabase&#125;可能会成功，此时，只读数据库对象会被关闭，返回读写对象。</span><br><span class="line">     * @throws 如果数据库不能打开，会抛出SQLiteException。</span><br><span class="line">     * @return 在调用&#123;@link #getWritableDatabase&#125;或&#123;@link #close&#125;之前，数据库对象是有效的。</span><br><span class="line">     */</span><br><span class="line">    public synchronized SQLiteDatabase getReadableDatabase() &#123;</span><br><span class="line">        if (mDatabase != null &amp;&amp; mDatabase.isOpen()) &#123;</span><br><span class="line">            return mDatabase;  // 数据库已经被打开了。</span><br><span class="line">        &#125;</span><br><span class="line">        if (mIsInitializing) &#123;</span><br><span class="line">            throw new IllegalStateException(&quot;getReadableDatabase called recursively&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        try &#123;</span><br><span class="line">            return getWritableDatabase();</span><br><span class="line">        &#125; catch (SQLiteException e) &#123;</span><br><span class="line">            if (mName == null) throw e;  // 不能以只读方式打开缓存数据库！</span><br><span class="line">            Log.e(TAG, &quot;Couldn&#x27;t open &quot; + mName + &quot; for writing (will try read-only):&quot;, e);</span><br><span class="line">        &#125;</span><br><span class="line">        SQLiteDatabase db = null;</span><br><span class="line">        try &#123;</span><br><span class="line">            mIsInitializing = true;</span><br><span class="line">            String path = mContext.getDatabasePath(mName).getPath();</span><br><span class="line">            db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);</span><br><span class="line">            if (db.getVersion() != mNewVersion) &#123;</span><br><span class="line">                throw new SQLiteException(&quot;Can&#x27;t upgrade read-only database from version &quot; +</span><br><span class="line">                        db.getVersion() + &quot; to &quot; + mNewVersion + &quot;: &quot; + path);</span><br><span class="line">            &#125;</span><br><span class="line">            onOpen(db);</span><br><span class="line">            Log.w(TAG, &quot;Opened &quot; + mName + &quot; in read-only mode&quot;);</span><br><span class="line">            mDatabase = db;</span><br><span class="line">            return mDatabase;</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            mIsInitializing = false;</span><br><span class="line">            if (db != null &amp;&amp; db != mDatabase) db.close();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>




<h4 id="源码如下："><a href="#源码如下：" class="headerlink" title="源码如下："></a>源码如下：</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">     * Create and/or open a database that will be used for reading and writing.</span><br><span class="line">     * Once opened successfully, the database is cached, so you can call this</span><br><span class="line">     * method every time you need to write to the database.  Make sure to call</span><br><span class="line">     * &#123;@link #close&#125; when you no longer need it.</span><br><span class="line">     *</span><br><span class="line">     * &lt;p&gt;Errors such as bad permissions or a full disk may cause this operation</span><br><span class="line">     * to fail, but future attempts may succeed if the problem is fixed.&lt;/p&gt;</span><br><span class="line">     *</span><br><span class="line">     * @throws SQLiteException if the database cannot be opened for writing</span><br><span class="line">     * @return a read/write database object valid until &#123;@link #close&#125; is called</span><br><span class="line">     */</span><br><span class="line">    public synchronized SQLiteDatabase getWritableDatabase() &#123;</span><br><span class="line">        if (mDatabase != null &amp;&amp; mDatabase.isOpen() &amp;&amp; !mDatabase.isReadOnly()) &#123;</span><br><span class="line">            return mDatabase;  // The database is already open for business</span><br><span class="line">        &#125;</span><br><span class="line">        if (mIsInitializing) &#123;</span><br><span class="line">            throw new IllegalStateException(&quot;getWritableDatabase called recursively&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        // If we have a read-only database open, someone could be using it</span><br><span class="line">        // (though they shouldn&#x27;t), which would cause a lock to be held on</span><br><span class="line">        // the file, and our attempts to open the database read-write would</span><br><span class="line">        // fail waiting for the file lock.  To prevent that, we acquire the</span><br><span class="line">        // lock on the read-only database, which shuts out other users.</span><br><span class="line">        boolean success = false;</span><br><span class="line">        SQLiteDatabase db = null;</span><br><span class="line">        if (mDatabase != null) mDatabase.lock();</span><br><span class="line">        try &#123;</span><br><span class="line">            mIsInitializing = true;</span><br><span class="line">            if (mName == null) &#123;</span><br><span class="line">                db = SQLiteDatabase.create(null);</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                db = mContext.openOrCreateDatabase(mName, 0, mFactory);</span><br><span class="line">            &#125;</span><br><span class="line">            int version = db.getVersion();</span><br><span class="line">            if (version != mNewVersion) &#123;</span><br><span class="line">                db.beginTransaction();</span><br><span class="line">                try &#123;</span><br><span class="line">                    if (version == 0) &#123;</span><br><span class="line">                        onCreate(db);</span><br><span class="line">                    &#125; else &#123;</span><br><span class="line">                        onUpgrade(db, version, mNewVersion);</span><br><span class="line">                    &#125;</span><br><span class="line">                    db.setVersion(mNewVersion);</span><br><span class="line">                    db.setTransactionSuccessful();</span><br><span class="line">                &#125; finally &#123;</span><br><span class="line">                    db.endTransaction();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            onOpen(db);</span><br><span class="line">            success = true;</span><br><span class="line">            return db;</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            mIsInitializing = false;</span><br><span class="line">            if (success) &#123;</span><br><span class="line">                if (mDatabase != null) &#123;</span><br><span class="line">                    try &#123; mDatabase.close(); &#125; catch (Exception e) &#123; &#125;</span><br><span class="line">                    mDatabase.unlock();</span><br><span class="line">                &#125;</span><br><span class="line">                mDatabase = db;</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                if (mDatabase != null) mDatabase.unlock();</span><br><span class="line">                if (db != null) db.close();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     * Create and/or open a database.  This will be the same object returned by</span><br><span class="line">     * &#123;@link #getWritableDatabase&#125; unless some problem, such as a full disk,</span><br><span class="line">     * requires the database to be opened read-only.  In that case, a read-only</span><br><span class="line">     * database object will be returned.  If the problem is fixed, a future call</span><br><span class="line">     * to &#123;@link #getWritableDatabase&#125; may succeed, in which case the read-only</span><br><span class="line">     * database object will be closed and the read/write object will be returned</span><br><span class="line">     * in the future.</span><br><span class="line">     *</span><br><span class="line">     * @throws SQLiteException if the database cannot be opened</span><br><span class="line">     * @return a database object valid until &#123;@link #getWritableDatabase&#125;</span><br><span class="line">     *     or &#123;@link #close&#125; is called.</span><br><span class="line">     */</span><br><span class="line">    public synchronized SQLiteDatabase getReadableDatabase() &#123;</span><br><span class="line">        if (mDatabase != null &amp;&amp; mDatabase.isOpen()) &#123;</span><br><span class="line">            return mDatabase;  // The database is already open for business</span><br><span class="line">        &#125;</span><br><span class="line">        if (mIsInitializing) &#123;</span><br><span class="line">            throw new IllegalStateException(&quot;getReadableDatabase called recursively&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        try &#123;</span><br><span class="line">            return getWritableDatabase();</span><br><span class="line">        &#125; catch (SQLiteException e) &#123;</span><br><span class="line">            if (mName == null) throw e;  // Can&#x27;t open a temp database read-only!</span><br><span class="line">            Log.e(TAG, &quot;Couldn&#x27;t open &quot; + mName + &quot; for writing (will try read-only):&quot;, e);</span><br><span class="line">        &#125;</span><br><span class="line">        SQLiteDatabase db = null;</span><br><span class="line">        try &#123;</span><br><span class="line">            mIsInitializing = true;</span><br><span class="line">            String path = mContext.getDatabasePath(mName).getPath();</span><br><span class="line">            db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);</span><br><span class="line">            if (db.getVersion() != mNewVersion) &#123;</span><br><span class="line">                throw new SQLiteException(&quot;Can&#x27;t upgrade read-only database from version &quot; +</span><br><span class="line">                        db.getVersion() + &quot; to &quot; + mNewVersion + &quot;: &quot; + path);</span><br><span class="line">            &#125;</span><br><span class="line">            onOpen(db);</span><br><span class="line">            Log.w(TAG, &quot;Opened &quot; + mName + &quot; in read-only mode&quot;);</span><br><span class="line">            mDatabase = db;</span><br><span class="line">            return mDatabase;</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            mIsInitializing = false;</span><br><span class="line">            if (db != null &amp;&amp; db != mDatabase) db.close();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2017/05/12/%E5%8A%A8%E6%80%81%E6%9B%B4%E6%8D%A2%E5%BA%94%E7%94%A8%E5%9B%BE%E6%A0%87%EF%BC%9Aactivity-alias%E8%AF%A6%E8%A7%A3%E5%8F%8A%E5%BA%94%E7%94%A8/" rel="prev" title="动态更换应用图标：activity-alias详解及应用">
      <i class="fa fa-chevron-left"></i> 动态更换应用图标：activity-alias详解及应用
    </a></div>
      <div class="post-nav-item">
    <a href="/2017/05/12/Kotlin%E6%98%AF%E4%B8%AA%E4%BB%80%E4%B9%88%E4%B8%9C%E4%B8%9C%EF%BC%9F/" rel="next" title="Kotlin是个什么东东？">
      Kotlin是个什么东东？ <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%A7%A3%E6%9E%90"><span class="nav-number">1.</span> <span class="nav-text">解析</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%99%E6%98%AF%E6%88%91%E7%BF%BB%E8%AF%91%E4%B9%8B%E5%90%8E%E7%9A%84"><span class="nav-number">2.</span> <span class="nav-text">这是我翻译之后的~~</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%BA%90%E7%A0%81%E5%A6%82%E4%B8%8B%EF%BC%9A"><span class="nav-number">3.</span> <span class="nav-text">源码如下：</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
